package com.sf.pathmemorymethod;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.sf.adapter.NumDefineAdapter;
import com.sf.adapter.PileEditAdapter;
import com.sf.bean.NumDefineItem;
import com.sf.util.Constant;
import com.sf.util.Mysqlitehelper;
import com.sf.util.Pile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by liutaigang on 2018/7/7.
 */

public class NumberDefineActivity extends Activity {

    private ListView listView;
    private List<NumDefineItem> numDefineList = new ArrayList<NumDefineItem>();
    private NumDefineAdapter nAdapter;
    private SQLiteOpenHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_num_define);

        listView = (ListView) findViewById(R.id.lv_num_define);

        initNumDefine();

        // 更改后刷新页面
        IntentFilter filter = new IntentFilter("action.NumberDefineActivity");
        registerReceiver(mRefreshBroadcastReceiver, filter);

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent intent = new Intent(NumberDefineActivity.this,
                        NumberDefineFixActivity.class);
                int id = view.getId();
                //Toast.makeText(NumberDefineActivity.this, "count = "+id, Toast.LENGTH_LONG).show();
                intent.putExtra("id", id);
                startActivity(intent);
                // 这里一定要改为true，代表长按自己消费掉了，若为false，触发长按事件的同时，还会触发点击事件
                return true;
            }
        });
    }


    // 更改后刷新页面
    BroadcastReceiver mRefreshBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("action.NumberDefineActivity"))
            {
                refresh();
            }
        }
    };

    public void refresh() {

        onCreate(null);

    }

    private void initNumDefine() {
        numDefineList = new ArrayList<NumDefineItem>();

        helper = new Mysqlitehelper(NumberDefineActivity.this);
        SQLiteDatabase db = helper.getWritableDatabase();
        //ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String,Object>>();
        Cursor cursor = db.query(Constant.TABLE_NUM,null,null,null,null,null,null);
        if (cursor == null){
            Log.i("tag","没有数据");
        }else {
            while (cursor.moveToNext()) {
                numDefineList.add(new NumDefineItem(
                        cursor.getInt(cursor.getColumnIndex(Constant.ID)),
                        cursor.getString(cursor.getColumnIndex(Constant.NUM)),
                        cursor.getString(cursor.getColumnIndex(Constant.NAME)),
                        cursor.getString(cursor.getColumnIndex(Constant.IMG_URL))));
            }
        }
        db.close();

        nAdapter = new NumDefineAdapter(this, numDefineList);
        listView.setAdapter(nAdapter);
    }
}